"""
简化版API - 无需insightface也能启动
用于测试HTML页面和基础功能
"""
from pathlib import Path
from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from typing import Optional

app = FastAPI(
    title="人脸质量检测API - 简化版",
    version="1.0.0-lite",
    description="简化版API服务（用于测试）"
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/", response_class=HTMLResponse)
async def root():
    """返回HTML调试页面"""
    html_path = Path("static/debug.html")
    if html_path.exists():
        return html_path.read_text(encoding="utf-8")
    return """
    <html>
        <head><meta charset="UTF-8"><title>人脸质量检测API</title></head>
        <body>
            <h1>人脸质量检测API - 简化版</h1>
            <p>⚠️ 这是简化版本，完整功能需要安装 insightface</p>
            <p>API文档: <a href="/docs">/docs</a></p>
        </body>
    </html>
    """

@app.get("/health")
async def health_check():
    """健康检查"""
    return {
        "status": "healthy",
        "version": "1.0.0-lite",
        "model_ready": False,
        "message": "简化版本 - 需要安装insightface才能使用完整功能"
    }

@app.get("/config")
async def get_config():
    """获取配置"""
    return {
        "model_name": "buffalo_l",
        "min_face_size": 100,
        "max_angle": 15.0,
        "blur_threshold": 60.0,
        "status": "lite_version"
    }

@app.post("/detect")
async def detect_face(
    file: UploadFile = File(...),
    min_face_size: Optional[int] = Form(None),
    max_angle: Optional[float] = Form(None),
    blur_threshold: Optional[float] = Form(None)
):
    """人脸检测接口（简化版）"""
    return {
        "success": False,
        "message": "⚠️ 需要安装 insightface 才能使用人脸检测功能",
        "data": {
            "file_received": file.filename,
            "file_size": len(await file.read()),
            "note": "请安装 insightface: pip install insightface"
        }
    }

@app.post("/detect/url")
async def detect_from_url(url: str = Form(...)):
    """URL检测接口（简化版）"""
    return {
        "success": False,
        "message": "⚠️ 需要安装 insightface 才能使用人脸检测功能",
        "data": {
            "url_received": url,
            "note": "请安装 insightface: pip install insightface"
        }
    }

if __name__ == "__main__":
    import uvicorn
    
    print("=" * 70)
    print("🚀 人脸质量检测API - 简化版")
    print("=" * 70)
    print()
    print("⚠️  这是简化版本，仅用于测试HTML页面和基础功能")
    print("    完整的人脸检测功能需要安装 insightface")
    print()
    print("安装 insightface 的方法：")
    print("  1. 安装 Visual Studio Build Tools")
    print("  2. pip install insightface")
    print()
    print("或查看: 简化启动说明.md")
    print("=" * 70)
    print()
    print("🌐 访问地址:")
    print("  Web界面: http://localhost:8000")
    print("  API文档: http://localhost:8000/docs")
    print()
    print("=" * 70)
    print()
    
    uvicorn.run(app, host="0.0.0.0", port=8000)

